package com.shxt.framework.rbac.weeklycontent.web;

import com.shxt.framework.rbac.weeklycontent.model.WeeklyContent;
import com.shxt.framework.rbac.weeklycontent.service.WeeklyContentService;
import com.shxt.framework.rbac.weeklycontent.service.impl.ExcelWriteService;
import jxl.write.WriteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 文件操作
 * @date 2019-12-05
 * @author stevenjob
 */
@Controller
@RequestMapping("/sys/weeklycontent")
public class FileController {
    @Autowired
    private WeeklyContentService weeklyContentService;

    /**
     * 页面使用sample:
     * <a href="/oa/sys/weeklycontent/exportExcel">导出文件</a>
     * @param response
     * @param request
     * @throws IOException
     * @throws WriteException
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request) throws IOException, WriteException {
        List<WeeklyContent> list = weeklyContentService.getAllWeeklyContentList();
        //3.设置响应头，提示浏览器不要解析响应的文件数据，而是以附件(attachment)的形式解析，即下载功能
        String filename = "周报数据.xls";
        response.setContentType(request.getServletContext().getMimeType(filename));
        response.setHeader("Content-Disposition", "attachment;filename="+filename);
        ExcelWriteService.createExcel(response.getOutputStream(), list);
    }
}
